package com.gfxy.common.portal.service;

import com.gfxy.common.portal.dao.SysOrganizationMapper;
import com.gfxy.common.portal.dao.SysOrganizationTypeMapper;
import com.gfxy.common.portal.entity.SysOrganization;
import com.gfxy.common.portal.entity.SysOrganizationExample;
import com.gfxy.common.portal.entity.SysOrganizationType;
import com.gfxy.common.portal.entity.SysOrganizationTypeExample;
import com.gfxy.commons.model.StateEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: lijixin 2019-05-17
 */
@Service
public class OrganizationService {

    private static Logger logger = LoggerFactory.getLogger(OrganizationService.class);

    @Autowired
    private SysOrganizationMapper organizationMapper;

    @Autowired
    private SysOrganizationTypeMapper organizationTypeMapper;


    public List<SysOrganization> findAllByParentId(String organizationId) {

        SysOrganizationExample example = new SysOrganizationExample();
        example.createCriteria().andParentIdEqualTo(organizationId);

        return organizationMapper.selectByExample(example);
    }

    public List<SysOrganization> findAll() {

        return organizationMapper.selectByExample(null);
    }

    public SysOrganization update(SysOrganization organization){
        logger.debug("部门信息为：{} , 准备更新",organization);
        organizationMapper.updateByPrimaryKey(organization);
        return organization;
    }

    public SysOrganization insert(SysOrganization organization) {

        logger.debug("部门信息为：{} , 准备新增,");
        organizationMapper.insert(organization);
        return organization;

    }


    public List<SysOrganizationType> queryOranizationType(){
        SysOrganizationTypeExample example = new SysOrganizationTypeExample();
        example.createCriteria().andStateEqualTo(StateEnum.VALID.getIndex());
        return organizationTypeMapper.selectByExample(example);
    }


    public List<SysOrganization> getOrganizationByType(String type) {
        SysOrganizationExample example = new SysOrganizationExample();
        example.createCriteria().andTypeEqualTo(Integer.parseInt(type));
        return organizationMapper.selectByExample(example);
    }
}
